<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class AdminConfig extends Model
{
    // 后台配置
    protected $table = 'admin_config';

    /**
     * 生成记录
     *
     * @param string $title
     * @param string $content
     * @param string $remark
     */
    public static function generateLog(string $title, string $content, string $remark = ''): void
    {
        $insert = [
            'title'   => $title,
            'content' => $content,
            'remark'  => $remark
        ];
        self::insert($insert);
    }// generateLog() end

    /**
     * set admin_config.content
     *
     * @param string $title
     * @param string $content
     * @param string $remark
     */
    public static function setContent(string $title, string $content, string $remark = ''): void
    {
        $res = self::whereRaw('title = :title', ['title' => $title])->findOrEmpty();
        if ($res->isExists())
        {
            $res->content = $content;
            $res->remark  = $remark;
            $res->save();
        } else {
            self::generateLog($title, $content, $remark);
        }
    }// setContent() end

    /**
     * @param string $title admin_config.title
     * @return string
     */
    public static function getContent(string $title): string
    {
        return self::whereRaw('title = :title', ['title' => $title])->value('content') ?? "";
    }// getContent() end
}
